package com.my.mall.manage.goods.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.my.mall.manage.goods.dao.GoodsManageDao;
import com.my.mall.manage.goods.dto.image.GoodsImageManageDTO;
import com.my.mall.manage.goods.dto.info.GoodsInfoManageDTO;
import com.my.mall.manage.goods.vo.category.GoodsCategoryVo;
import com.my.mall.manage.goods.vo.image.GoodsImageManageVo;
import com.my.mall.manage.goods.vo.info.GoodsInfoVo;
import com.my.mall.manage.goods.vo.sold.GoodsSoldManageVo;
import com.my.mall.manage.order.vo.orderInfoItems.OrderInfoItemsManageVo;

@Service
public class GoodsManageService {

	@Autowired
	GoodsManageDao goodsManageDao;

	public List<Map<String, Object>> selectCategoryList(GoodsCategoryVo pv, List<Map<String, Object>> listmap,
			List<GoodsCategoryVo> list) {

		if (listmap == null || list == null) {
			list = goodsManageDao.selectCategoryList(pv.getTopId());
			listmap = new ArrayList<Map<String, Object>>();
			for (GoodsCategoryVo p : list) {
				if (p.getTopId() == 0) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("id", p.getId());
					map.put("text", p.getCategoryName());
					listmap.add(map);
				}
			}
			selectCategoryList(pv, listmap, list);

		} else if (listmap.size() > 0 && list.size() > 0) {
			for (Map<String, Object> mp : listmap) {
				List<Map<String, Object>> childlist = new ArrayList<Map<String, Object>>();
				String id = mp.get("id") + "";
				list = goodsManageDao.selectCategoryList(Integer.parseInt(id));
				for (GoodsCategoryVo p : list) {
					Map<String, Object> m = new HashMap<String, Object>();
					m.put("id", p.getId());
					m.put("text", p.getCategoryName());
					childlist.add(m);
				}
				if (childlist.size() > 0) {
					List<String> sizelist = new ArrayList<String>();
					sizelist.add(childlist.size() + "");
					mp.put("nodes", childlist);
					mp.put("tags", sizelist);
					selectCategoryList(pv, childlist, list);
				}
			}
		}
		return listmap;
	}

	public int updateCategory(int id, String categoryName, int topId, int imgId) {
		return goodsManageDao.updateCategory(id, categoryName, topId, imgId);
	}

	public int saveCategory(String categoryName, int topId, int imgId) {
		return goodsManageDao.saveCategory(categoryName, topId, imgId);
	}

	public GoodsCategoryVo getCategory(int id) {
		GoodsCategoryVo categoryDate = goodsManageDao.getCategory(id);
		GoodsCategoryVo topDate = goodsManageDao.getCategory(categoryDate.getTopId());
		if (topDate != null) {
			categoryDate.setTopName(topDate.getCategoryName());
		}
		return categoryDate;
	}

	public int delCategory(int id) {
		if (goodsManageDao.selectCategoryList(id).size() > 0) {
			return 2;
		}
		return goodsManageDao.delCategory(id);
	}

	public int selectInfoCount() {
		return goodsManageDao.selectInfoCount();
	}

	public List<GoodsInfoVo> selectInfoList(int iDisplayStart, int iDisplayLength) {
		List<GoodsInfoVo> infos = goodsManageDao.selectInfoList(iDisplayStart, iDisplayLength);
		for (GoodsInfoVo info : infos) {
			GoodsCategoryVo category = getCategory(info.getCategoryID());
			if (category != null) {
				info.setCategoryName(category.getCategoryName());
			}
		}
		return infos;
	}

	public int updateInfo(GoodsInfoManageDTO info) {
		return goodsManageDao.updateInfo(info.getId(), info.getCategoryID(), info.getName(), info.getTitle(),
				info.getSynopsis(), info.getIsBest(), info.getIsHot(), info.getIsNew(), info.getIsFree(),
				info.getSell(), info.getBestCount(), info.getMediumCount(), info.getBadCount());
	}

	public int saveInfo(GoodsInfoManageDTO info) {
		return goodsManageDao.saveInfo(info.getCategoryID(), info.getName(), info.getTitle(), info.getSynopsis(),
				info.getIsBest(), info.getIsHot(), info.getIsNew(), info.getIsFree(), info.getSell(),
				info.getBestCount(), info.getMediumCount(), info.getBadCount());
	}

	public GoodsInfoVo getInfo(int id) {
		GoodsInfoVo info = goodsManageDao.getInfo(id);
		if (info != null) {
			GoodsCategoryVo category = getCategory(info.getCategoryID());
			if (category != null) {
				info.setCategoryName(category.getCategoryName());
			}
		}
		return info;
	}

	public GoodsInfoVo getInfoByGoodsName(String GoodsName) {
		return goodsManageDao.getInfoByGoodsName(GoodsName);
	}

	public int delInfo(int id) {
		return goodsManageDao.delInfo(id);
	}

	public int updateShelves(GoodsInfoManageDTO info) {
		return goodsManageDao.updateShelves(info.getId(), info.getFavorable(), info.getShopPrice(), info.getCostPrice(),
				info.getInventory(), info.getWarning(), info.getIsShelves());
	}

	public List<OrderInfoItemsManageVo> getGoodsNameById(List<OrderInfoItemsManageVo> list) {
		for (OrderInfoItemsManageVo orderInfoItems : list) {
			GoodsInfoVo info = goodsManageDao.getInfo(orderInfoItems.getGoodsId());
			orderInfoItems.setGoodsName(info.getName());
		}
		return list;
	}
	/*
	 * public int saveShelves(GoodsInfoManageDTO info) { return
	 * goodsManageDao.saveShelves(info.getFavorable(), info.getShopPrice(),
	 * info.getCostPrice(), info.getInventory(), info.getWarning(),
	 * info.getIsShelves()); }
	 */

	public List<GoodsSoldManageVo> selectSoldInfoList(int iDisplayStart, int iDisplayLength) {
		return goodsManageDao.selectSoldInfoList(iDisplayStart, iDisplayLength);
	}

	public int selectSoldInfoCount() {
		return goodsManageDao.selectSoldInfoCount();
	}

	public int updateImageInfo(GoodsImageManageDTO imgInfo) {
		int imgNum1 = goodsManageDao.updateImage(imgInfo.getImgId1(), imgInfo.getGoodsId());
		int imgNum2 = goodsManageDao.updateImage(imgInfo.getImgId2(), imgInfo.getGoodsId());
		int imgNum3 = 0;
		String[] imgIds3 = imgInfo.getImgId3().split(",");
		for (String imgId : imgIds3) {
			imgNum3 += goodsManageDao.updateImage(Integer.parseInt(imgId), imgInfo.getGoodsId());
		}
		return imgNum1 + imgNum2 + imgNum3;
	}

	public List<GoodsImageManageVo> selectImageInfoList(int goodsId) {
		return goodsManageDao.selectImageInfoList(goodsId);
	}

	public int delImg(int id) {
		return goodsManageDao.delImg(id);
	}

}
